/*
 * CLProtocol.h
 *
 *  Created on: May 20, 2011
 *      Author: ulas
 */

#ifndef CLPROTOCOL_H_
#define CLPROTOCOL_H_

#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <errno.h>

#include "rtOpts.h"

enum {
	IL2CLREQRT,
	IL_MODULE,
	LATENCY_ARRAY_COUNT
};

class CLProtocol {
private:
	rtOpts* opts;
	U8 notRTSlot[2];
	U8 notMyRTSlot[2];
	U8 dbgCnt;

protected:
	U8 NODE_ID;

public:
	U32 curLatency[LATENCY_ARRAY_COUNT];
	U32 maxLatency[LATENCY_ARRAY_COUNT];
	U32 minLatency[LATENCY_ARRAY_COUNT];
	U32 avgLatency[LATENCY_ARRAY_COUNT];
	U32 cntLatency[LATENCY_ARRAY_COUNT];
	bool updatedLatency[LATENCY_ARRAY_COUNT];


	CLProtocol(U8 nodeId);

	virtual STATUS gUpdate(const U8* msg, const U16 messageLength) = 0;
	virtual STATUS gRealTime(SLOT_TYPE* RTCL) =0;
	virtual STATUS gMySlot(myCL_TYPE* myCL) =0;
	virtual U16 prepareCLHeader(U8* msg, void* parPtr) = 0;
	virtual U8 getCLHeaderLength(U8* msg) = 0;
	virtual void updateCountForDART() = 0;


	STATUS run();
	void updateLatencyData(U8 latencyArrSelect, TimeInternal* tdiff);
	U8 selectMsgQ(TimeInternal* timeout, int dev_file);
	STATUS processAP2CL(U8* msg, U16 payloadLength);
	STATUS processIL2CLRT(const U8* msg, const U16 msgLength);
	STATUS processREQRT();
	STATUS CL2ILRT();

	virtual ~CLProtocol();
};

#endif /* CLPROTOCOL_H_ */
